package cn.edu.hitwh.kanlema.util;

import cn.edu.hitwh.kanlema.dto.UserDTO;
import cn.edu.hitwh.kanlema.exception.UnauthorizedException;

public class UserHolder {
    private static final ThreadLocal<UserDTO> tl = new ThreadLocal<>();

    public static void saveUser(UserDTO user){
        tl.set(user);
    }

    public static UserDTO getUser(){
        UserDTO userDTO = tl.get();
        if (userDTO == null) {
            throw new UnauthorizedException();
        }
        return userDTO;
    }

    public static UserDTO tryGetUser(){
        return tl.get();
    }

    public static void removeUser(){
        tl.remove();
    }
}
